<template>
<div>
<!-- 面包屑导航区域 -->
<el-breadcrumb separator-class="el-icon-arrow-right">
  <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
  <el-breadcrumb-item>用户管理</el-breadcrumb-item>
  <el-breadcrumb-item>用户列表</el-breadcrumb-item>
</el-breadcrumb>
<!-- 卡片视图区 -->
<el-card >
    <!-- 搜索与添加区域 -->
    <el-row :gutter="20">
        <el-col :span="7">
            <el-input placeholder="请输入内容" v-model="form.username" clearable @clear="userlist">
                <el-button slot="append" icon="el-icon-search" @click="userlist"></el-button>
            </el-input>
        </el-col>
        <el-col :span="4">
            <el-button type="primary" @click="toAdd">添加用户</el-button>
        </el-col>
    </el-row> 
    <!-- 用户列表区域 -->
    <el-table :data="userlist" border stripe>
      <el-table-column label="#" type="index"></el-table-column>
      <el-table-column label="姓名" prop="username"></el-table-column>
      <el-table-column label="密码" prop="password"></el-table-column>
      <el-table-column label="电话" prop="telephone"></el-table-column>
      <el-table-column label="性别" prop="gender"></el-table-column>
      <el-table-column label="昵称" prop="realname"></el-table-column>
      <el-table-column label="操作">
           <template slot-scope="scope">
               <!-- 修改按钮 -->
               <el-button type="primary" icon="el-icon-edit" size="mini"  @click="edit(scope.row)"></el-button>
               <!-- 删除按钮 -->
               <el-button type="danger" icon="el-icon-delete" size="mini" @click="del(scope.row)"></el-button>
           </template>
      </el-table-column>
  </el-table>
    <!-- 模态框 -->
    <el-dialog
      class="customer_mobal"
      title="添加用户"
      :visible.sync="dialogVisible"
      width="60%">
            <el-form :model="form" label-width="100px">
        <el-form-item label="姓名" >
           <el-input v-model="form.username" ></el-input>
        </el-form-item>
        <el-form-item label="性别" >
           <el-input v-model="form.gender" ></el-input>
        </el-form-item>
    </el-form>
        <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="save" size="small">确 定</el-button>
      </span>
    </el-dialog>
</el-card>
</div>
</template>
<script>
import { get, post } from '@/utils/request'
export default {
    data () {
        return {
             // 添加用户的表单数据
      form: {
        username: '',
        password: '',
        telephone: '',
        gender: '',
        realname: ''
      },
      // 查询到的表单数据
      userlist: [],
      dialogVisible: false
        }
    },
    //请求数据
    created() {
        this.loadDate();
    },
//方法表示一个具体的操作，主要书写业务逻辑；
 methods: {
     save(){
          let url = "/user/saveOrUpdate"
          post(url, this.form).then((response) => {
              this.dialogVisible = false;
              this.$message({ type: "success", message: response.message })
              this.loadDate();
          })
      },
      del(row) {
        let url = "/user/delete"
        get(url, { id: row.id }).then((response) => {
        this.$message({ type: "success", message: response.message })
        this.loadDate();
       })
    },
      edit(row) {
        this.form = row;
        this.dialogVisible = true;
      },
       loadDate() {
          let url = "/user/findAll";
          get(url).then((response) => {
               this.userlist = response.data;
                 console.log(this.userlist)
          });
       },
    toAdd() {
      this.dialogVisible = true;
    } 
 },

}
</script>

<style scoped>

</style>
